package com.woniu.permissions.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.model.Result;
import com.woniu.permissions.form.AddRoleResourceForm;
import com.woniu.permissions.model.RoleResource;
import com.woniu.permissions.param.AddRoleResourceParam;
import com.woniu.permissions.service.RoleResourceService;
import com.woniu.util.RedisUtil;
import com.woniu.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/roleResource")
@Api(tags = "角色资源关联服务")
public class RoleResourceController {
    @Resource
    private RoleResourceService roleResourceService;
    @Resource
    private RedisUtil redisUtil;


    @ApiOperation("/新增角色资源关联")
    @PostMapping("/addRoleResource")
    public Result<RoleResource> addRoleResource(@RequestBody AddRoleResourceForm form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        RoleResource roleResource = roleResourceService.addRoleResource(BeanUtil.toBean(form, AddRoleResourceParam.class));
        return Result.newInstance(RoleResource.class).setData(roleResource).setMessage("新增角色资源关联成功");
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除角色资源关联数据")
    @GetMapping("delRoleResource")
    public Result delRoleResource(@PathVariable("id") @ApiParam(name = "id",value = "表id") Long id,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        RoleResource roleResource = roleResourceService.delRoleResource(id);
        return Result.newInstance(RoleResource.class).setData(roleResource).setMessage("删除成功");
    }

}

